home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / tstcom.lqr / TSTCOM.LBR / tstcom2.c < prev   
Text File  |  2011-02-04  |  4KB  |  124 lines

  1. /*
  2.  * tstcom2.c  31 Oct 83  Craig Milo Rogers at USC/ISI
  3.  *
  4.  *    This program tests the COM: communications package
  5.  * by using it as a TTY Telnet box.  Both COM: ports are used
  6.  * for a single connection.  COM1: is the user port, and COM2:
  7.  * is the server port.
  8.  */
  9.  
  10. #define M_TSTCOM
  11.  
  12. #include "stdio.h"
  13.  
  14. #include "truth.h"
  15. #include "beauty.h"
  16.  
  17. #define TNESCAPE 0036        /* "Telnet" escape char:  control-uparrow. */
  18.  
  19. #define RBUFLEN 4096        /* Receive buffer length. */
  20. #define TBUFLEN 512        /* Transmit buffer length. */
  21. static char rbuf1[RBUFLEN];    /* Receive data buffer. */
  22. static char tbuf1[TBUFLEN];    /* Transmit data buffer. */
  23. static char rbuf2[RBUFLEN];    /* Receive data buffer. */
  24. static char tbuf2[TBUFLEN];    /* Transmit data buffer. */
  25.  
  26. main(argc,argv)
  27. int argc;            /* Number of arguments. */
  28. char *argv[];            /* Pointers to argument strings. */
  29. {
  30.     int rate;            /* Baud rate in bps. */
  31.     int divisor;        /* Baud rage generator divisor. */
  32.       int c;            /* Holds a data character. */
  33.     bool sawesc;        /* Indicates that ctrl-q was last char. */
  34.     bool running;        /* FALSE indicates exit request. */
  35.     int i;            /* Counter. */
  36.     int unit;            /* COM: unit number. */
  37.  
  38.     if (argc < 2) {        /* Not enough args, give help message. */
  39.     fprintf(stderr, "usage:  tstcom2 baudrate\n");
  40.     exit(1);
  41.     }
  42.  
  43.     rate = 0;
  44.     sscanf(argv[1], "%d", &rate);
  45.     if ((divisor = getdivisor(rate)) == 0) {
  46.     fprintf(stderr, "tstcom2: Unrecognized baud rate.\n");
  47.     exit(1);
  48.     }
  49.  
  50.                 /* Initialize COM1: and COM2:. */
  51.     int_ini();            /* Start with interrupt package. */
  52.     com_ini(1, divisor, tbuf1, TBUFLEN, rbuf1, RBUFLEN);
  53.     com_ini(2, divisor, tbuf2, TBUFLEN, rbuf2, RBUFLEN);
  54.  
  55.     sawesc = FALSE;        /* Start off with normal data. */
  56.     running = TRUE;        /* Running the glass tty. */
  57.     unit = 2;            /* Destination is COM2:. */
  58.  
  59.     while (running) {        /* Until we decide to exit: */
  60.     if (com_icnt(1) != 0) {    /* Got a character? */
  61.         c = com_getc(1);    /* Pick up the character. */
  62.         if (sawesc) {    /* Did we just see a telnet escape? */
  63.         sawesc = FALSE;    /* Cancel the saw-escape flag. */
  64.         switch (c &0177) {    /* Dispatch to command processor. */
  65.  
  66.         case TNESCAPE:    /* A second escape char. */
  67.             com_putc(unit, c);    /* Pass it through. */
  68.             break;
  69.  
  70.         case 'b':    /* A request to send BREAK. */
  71.         case 'B':
  72.             com_break(unit);
  73.             break;
  74.  
  75.         case 'c':    /* The "close" command. */
  76.         case 'C':
  77.         case 'e':    /* The "exit" command. */
  78.         case 'E':
  79.         case 'q':    /* The "quit" command. */
  80.         case 'Q':
  81.             running = FALSE;
  82.             continue;
  83.  
  84.         default:    /* Complain about bad char. */
  85.             com_putc(1, 0007);    /* Ring the "bell". */
  86.             break;
  87.         }
  88.         } else {        /* Otherwise, didn't see escape prev. */
  89.         switch (c &0177) {    /* Dispatch to character processor. */
  90.  
  91.         case TNESCAPE:        /* Process "Telnet" escape char. */
  92.             sawesc = TRUE;    /* Set saw-escape flag. */
  93.             break;
  94.  
  95.         default:        /* Otherwise, send to COM1: as is. */
  96.             com_putc(unit, c);    /* Ignore errors. */
  97.         }
  98.         }
  99.     }
  100.     for (i = com_icnt(unit); i > 0; i--) {
  101.         c = com_getc(unit);    /* Get a COM2: character. */
  102.         com_putc(1, c);    /* Send to COM1:. */
  103.     }
  104.     }
  105.  
  106.     com_trm(1);            /* Restore interrupt vectors, etc. */
  107.     com_trm(2);
  108.     int_trm();
  109. }
  110.  
  111. int                /* Returns baud rate generator divisor. */
  112. getdivisor(rate)        /* Convert baud rate to BRG divisor. */
  113. int rate;            /* Baud rate. */
  114. {
  115.     switch (rate) {        /* Dispatch on baud rate. */
  116.     case 9600:    return (12);
  117.     case 4800:    return (24);
  118.     case 2400:    return (48);
  119.     case 1200:    return (96);
  120.     case 300:    return (384);
  121.     default:    return (0);    /* Unrecognized rate. */
  122.     }
  123. }
  124.